from odoo import fields, models, api
from odoo.exceptions import UserError


class RPWebContactUs(models.Model):
    _name = 'rp.web.contact.us'
    _description = "联系我们"
    _rec_name = "code"
    _sql_constraints = [('unique_name', 'UNIQUE(code)', '索引码重复')]
    _order = 'create_datetime desc'

    code = fields.Char(string='索引码', default='RP_CU/20240101/***', index=True, copy=False, readonly=True)
    create_datetime = fields.Datetime(string='创建时间', default=fields.Datetime.now, copy=False, readonly=True)

    first_name = fields.Char(string='名')
    last_name = fields.Char(string='姓')
    email = fields.Char(string='邮箱')
    phone = fields.Char(string='电话')
    type_id = fields.Many2one(string='类型', comodel_name='rp.web.contact.us.type')
    body = fields.Text(string='内容')

    @api.model_create_multi
    def create(self, vals):
        for val in vals:
            val['code'] = self.env['ir.sequence'].sudo().next_by_code('rp.web.contact.us') or '/'
        return super(RPWebContactUs, self).create(vals)
